package org.tomac.fix.example.fix42nordic;

import java.io.IOException;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.tomac.io.IIOPlexor;
import org.tomac.io.ITimerTask;
import org.tomac.io.fix.FixInitiator;
import org.tomac.io.fix.FixURL;
import org.tomac.io.fix.IFixApplication;
import org.tomac.io.fix.IFixSession;
import org.tomac.io.fix.IFixURL;
import org.tomac.io.nio.NIOPlexorTest;
import org.tomac.protocol.fix.messaging.FixMessageInfo;
import org.tomac.protocol.fix.messaging.FixMessageListener;
import org.tomac.protocol.fix.messaging.FixMessageListenerImpl;
import org.tomac.protocol.fix.messaging.FixNewOrderSingle;
import org.tomac.utils.Utils;

public class FixClient extends FixMessageListenerImpl implements IFixApplication, FixMessageListener {

	private static Logger log = Logger.getLogger(FixClient.class.getSimpleName());

	FixMessageListener fixSessionInboundFixMessageListener;
	FixInitiator session;
	NIOPlexorTest mux;
	IFixURL url;
	boolean forever = true;
	boolean isSendingOrders = false;

	static public void main(String[] args) {
		FixClient app;

		if (args.length < 1) {
			log.info("Usage: FixClient <fix_url>");
			log.info("Example: FixClient fix42nordic://TOMAC:TE@127.0.0.1:1212/?heartBtInt=60#initiator");
			return;
		}
		
		BasicConfigurator.configure();

		app = new FixClient();

		try {

			app.mux = new NIOPlexorTest();
			app.url = new FixURL(args[0]); // the static configuration
			// application = ...;
			app.session = new FixInitiator(app.mux, app.url, app); // the glue IO<->session

			app.session.connect();

			// either starta a thread or I run it in main
			app.mux.run();

		} catch (IOException e) {
			if (log.isDebugEnabled()) log.error(e.getMessage(), e);
			else log.error(e.getMessage());
		}
	}
	
	@Override
	public void init(IFixSession session, IIOPlexor plex) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDisconnect() {
		log.info("Got disconnect");
		forever = false;
	}

	@Override
	public void onConnect() {
		log.info("Got connect");
		ITimerTask taskSendOrder = new ITimerTask() {
			@Override
			public void run() {
				isSendingOrders = true;
				sendNewOrderSingle();		
			}

		};
		mux.scheduleTimer(taskSendOrder, 1000);
		ITimerTask taskQuit = new ITimerTask() {
			@Override
			public void run() {
				forever = false;
			}
		};
		mux.scheduleTimer(taskQuit, 2000);
	}

	@Override
	public FixMessageListener getInboundFixMessageReplicator() {
		// just pass on to session layer, that will forward to our inbound message listener
		return session.getInboundFixMessageListener();
	}

	@Override
	public void onFixNewOrderSingle(FixNewOrderSingle msg) {
		
		session.incrementInMsgSeqNum();

		byte[] buf = new byte[msg.buf.limit()];
		msg.buf.flip();
		msg.buf.slice().get(buf);
		if (log.isDebugEnabled()) log.debug("Got NewOrderSingle: " + new String(buf));
	}

	private void sendNewOrderSingle() {
		FixNewOrderSingle order = new FixNewOrderSingle();
		order.price = 1000000;
		order.orderQty = 1000;
		Utils.longToNumeric(order.clOrdID, 0, mux.currentTimeMillis(), order.clOrdID.length);
		order.side = FixMessageInfo.Side.BUY;
		order.minQty = 0;
		Utils.copy("VOLVO".getBytes(), 0, order.symbol, 0, "VOLVO".length());
		order.ordType = FixMessageInfo.OrdType.LIMIT;
		Utils.utcTimestampConverter.convertToUtcTimestamp(order.transactTime, mux.currentTimeMillis(), true);
		order.handlInst = FixMessageInfo.HandlInst.AUTOMATED_EXECUTION_ORDER_PRIVATE_NO_BROKER_INTERVENTION;
		session.send(order);
	}

}
